// Parse the time to string
export const parseTime = (
  time?: object | string | number,
  cFormat?: string
): string => {
  if (time === undefined || time === null || time === '') {
    return '-'
  }
  const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  let date: Date
  if (typeof time === 'object') {
    date = time as Date
  } else {
    if (typeof time === 'string' && /^[0-9]+$/.test(time)) {
      time = parseInt(time)
    }
    if (typeof time === 'number' && time.toString().length === 10) {
      time = time * 1000
    }
    date = new Date(time)
  }
  const formatObj: { [key: string]: number } = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  }
  const timeStr = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
    // result 为匹配模式的字符串，key为匹配规则中的字符串，有多少个则重复匹配多少个
    // 比如'{d}-{a}'第一次result为{d}，key则为d，第二次为{a},key为a，所以想要匹配2019/09/29的数据则匹配 {y}/{m}/{d} '星期{a}'
    let value = formatObj[key]
    // Note: getDay() returns 0 on Sunday
    if (key === 'a') {
      return ['日', '一', '二', '三', '四', '五', '六'][value]
    }
    if (result.length > 0 && value < 10) {
      return '0' + value
    }
    return String(value) || '0'
  })
  return timeStr
}
// 在一些js处理datepicker变化时不能返回-
export const parseTimeJS = (
  time?: object | string | number,
  cFormat?: string
): string => {
  if (time === undefined || time === null || time === '') {
    return ''
  }
  const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  let date: Date
  if (typeof time === 'object') {
    date = time as Date
  } else {
    if (typeof time === 'string' && /^[0-9]+$/.test(time)) {
      time = parseInt(time)
    }
    if (typeof time === 'number' && time.toString().length === 10) {
      time = time * 1000
    }
    date = new Date(time)
  }
  const formatObj: { [key: string]: number } = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  }
  const timeStr = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
    // result 为匹配模式的字符串，key为匹配规则中的字符串，有多少个则重复匹配多少个
    // 比如'{d}-{a}'第一次result为{d}，key则为d，第二次为{a},key为a，所以想要匹配2019/09/29的数据则匹配 {y}/{m}/{d} '星期{a}'
    let value = formatObj[key]
    // Note: getDay() returns 0 on Sunday
    if (key === 'a') {
      return ['日', '一', '二', '三', '四', '五', '六'][value]
    }
    if (result.length > 0 && value < 10) {
      return '0' + value
    }
    return String(value) || '0'
  })
  return timeStr
}
// Format and filter json data using filterKeys array
export const formatJson = (filterKeys: any, jsonData: any) =>
  jsonData.map((data: any) =>
    filterKeys.map((key: string) => {
      if (key === 'timestamp') {
        return parseTime(data[key])
      } else {
        return data[key]
      }
    })
  )
export const adjustScreen = (
  actualWidth = 0,
  actualHeight = 0,
  originWight = 1920,
  originHeight = 1080
) => {
  // app-main高度/宽度
  let screenHeight = actualHeight
  let screenWidth = actualWidth
  // dashboard高度/宽度
  let dashboardHeight, dashboardWidth
  if ((screenWidth * 9) / 16 >= screenHeight) {
    dashboardWidth = (screenHeight * 16) / 9
    dashboardHeight = screenHeight
  } else {
    dashboardWidth = screenWidth
    dashboardHeight = (screenWidth * 9) / 16
  }
  let scale =
    'scale(' + dashboardWidth / 1920 + ',' + dashboardHeight / 1080 + ')'
  return {
    dashboardStyle: {
      width: `${dashboardWidth}px`,
      height: `${dashboardHeight}px`
    },
    dashboardChildrenStyle: {
      '-webkit-transform': scale,
      '-webkit-transform-origin': '0 0',
      '-moz-transform': scale,
      '-moz-transform-origin': '0 0',
      '-ms-transform': scale,
      '-ms-transform-origin': '0 0',
      '-o-transform': scale,
      '-o-transform-origin': '0 0'
    }
  }
}
export const riskAdjustScreen = (
  actualWidth = 0,
  actualHeight = 0,
  originWight = 1920,
  originHeight = 1080
) => {
  // app-main高度/宽度
  let screenHeight = actualHeight
  let screenWidth = actualWidth
  // dashboard高度/宽度
  let dashboardHeight, dashboardWidth
  if ((screenWidth * 9) / 15 >= screenHeight) {
    dashboardWidth = (screenHeight * 15) / 9
    dashboardHeight = screenHeight
  } else {
    dashboardWidth = screenWidth
    dashboardHeight = (screenWidth * 9) / 15
  }
  let scale =
    'scale(' + dashboardWidth / 1920 + ',' + dashboardHeight / 1080 + ')'
  return {
    dashboardStyle: {
      width: `${dashboardWidth}px`,
      height: `${dashboardHeight}px`
    },
    dashboardChildrenStyle: {
      '-webkit-transform': scale,
      '-webkit-transform-origin': '0 0',
      '-moz-transform': scale,
      '-moz-transform-origin': '0 0'
    }
  }
}
